home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / fbm12s.lha / fbhalf.c < prev    next >
C/C++ Source or Header  |  1994-07-18  |  7KB  |  218 lines

  1. /*****************************************************************
  2.  * fbhalf.c: FBM Release 1.2 07-Apr-93 Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989-1993 by Michael Mauldin.  Permission is granted
  5.  * to use this file in whole or in part for any purpose, educational,
  6.  * recreational or commercial, provided that this copyright notice
  7.  * is retained unchanged.  This software is available to all free of
  8.  * charge by anonymous FTP and in the UUNET archives.
  9.  *
  10.  * fbhalf.c: Take an 8bit gray image, resize it to a maximum total
  11.  *          number of pixels, optionally sharpen it with a digital
  12.  *          Laplacian filter, and halftone it using one of three
  13.  *          standard algorithms.  Output the result in PBM format.
  14.  *
  15.  * USAGE
  16.  *    % fbhalf [ -args ]  [ size ] < 8bit > 1bit
  17.  *
  18.  *    size    Choose a width and height as large as possible so that
  19.  *        width is a factor of 8 and width*height <= size (default
  20.  *        is width and height of original 8bit file, ignoring aspect
  21.  *        ratio).
  22.  *
  23.  *    -f    Do Floyd-Steinberg halftoning (the default algorithm)
  24.  *    -b<int>    Do Blue noise halftoning (-b50 or 50% noise is default)
  25.  *    -c<int>    Do Constained average halftoning (-c4 is the default)
  26.  *    -s<int>    Sharpen the image with a given beta (-s2.0 is default)
  27.  *    -t<int>    Use a threshhold of <int> to halftone (127 is default)
  28.  *
  29.  *    -C<int>,-N<int>
  30.  *        Clean up image by flipping isolated pixels.  A pixel is
  31.  *        isolated if there are fewer than C like pixels in the
  32.  *        nearby NxN square.
  33.  *
  34.  * EDITLOG
  35.  *    LastEditDate = Mon Jun 25 00:02:44 1990 - Michael Mauldin
  36.  *    LastFileName = /usr2/mlm/src/misc/fbm/fbhalf.c
  37.  *
  38.  * HISTORY
  39.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  40.  *    Package for Release 1.0
  41.  *
  42.  * 03-May-89  Michael Mauldin (mlm) at Carnegie Mellon University
  43.  *    Beta release (version 0.93) mlm@cs.cmu.edu
  44.  *
  45.  *  8-Sep-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  46.  *    Created.
  47.  *****************************************************************/
  48.  
  49. # include <stdio.h>
  50. # include <math.h>
  51. # include "fbm.h"
  52.  
  53. # define USAGE\
  54. "Usage: fbhalf [ -fbct<parm> ] [-s<sharpen> ] [ -<type> ]\n\
  55.               [ -C<clean> -N<nbr>] [ size ] < 8bit > 1bit"
  56.  
  57. #ifndef lint
  58. static char *fbmid =
  59. "$FBM fbhalf.c <1.2> 07-Apr-93 (C) 1989-1993 by Michael Mauldin, source \
  60. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  61. #endif
  62.  
  63. main (argc, argv)
  64. char *argv[];
  65. { int w, h, ow = -1, oh = -1, size = -1, alg = 'b';
  66.   int clean = -1, nbr = 5, outtype = DEF_1BIT;
  67.   double beta = -1e9, parm = -1e9;
  68.   char *title, *credits;
  69.   FBM input, resized, sharpened, halftoned, cleaned, *image;
  70.  
  71.   /* Clear pointers */
  72.   input.bm     = input.cm     = (unsigned char *) NULL;
  73.   resized.bm   = resized.cm   = (unsigned char *) NULL;
  74.   sharpened.bm = sharpened.cm = (unsigned char *) NULL;
  75.   halftoned.bm = halftoned.cm = (unsigned char *) NULL;
  76.   cleaned.bm   = cleaned.cm   = (unsigned char *) NULL;
  77.  
  78.   if (read_bitmap (&input, (char *) NULL))
  79.   {
  80.     if (input.hdr.bits != 8 || input.hdr.physbits != 8)
  81.     { fprintf (stderr,
  82.            "Can't handle images with %d bits and %d physbits per pixel\n",
  83.            input.hdr.bits, input.hdr.physbits);
  84.       exit (1);
  85.     }
  86.  
  87.     if (input.hdr.title[0]) title = input.hdr.title;
  88.     if (input.hdr.credits[0]) credits = input.hdr.credits;
  89.  
  90.     /* Get the options */
  91.     while (--argc > 0 && (*++argv)[0] == '-')
  92.     { while (*++(*argv))
  93.       { switch (**argv)
  94.         { case 's':    if (argv[0][1]) { beta = atof (*argv+1); SKIPARG; }
  95.             else        beta = 2.0;
  96.             break;
  97.             
  98.       case 'f':    alg = 'f'; parm = 0.0; break;
  99.             
  100.       case 'b':    alg = 'b';
  101.             if (argv[0][1])    { parm = atof (*argv+1); SKIPARG; }
  102.             break;
  103.             
  104.       case 'c':    alg = 'c';
  105.             if (argv[0][1])    { parm = atof (*argv+1); SKIPARG; }
  106.             break;
  107.             
  108.       case 't':    alg = 't';
  109.             if (argv[0][1])    { parm = atoi (*argv+1); SKIPARG; }
  110.             else        { parm = 127; }
  111.             break;
  112.             
  113.       case 'C':    if (argv[0][1])    { clean = atoi (*argv+1); SKIPARG; }
  114.               else        { clean = 10; }
  115.             break;
  116.             
  117.       case 'N':    if (argv[0][1])    { nbr = atoi (*argv+1); SKIPARG; }
  118.               else        { nbr = 5; }
  119.             
  120.             if (clean < 0)    { clean = 10; }
  121.             break;
  122.             
  123.       case 'A':    outtype = FMT_ATK; break;
  124.       case 'B':    outtype = FMT_FACE; break;
  125.       case 'F':    outtype = FMT_FBM; break;
  126.       case 'G':    outtype = FMT_GIF; break;
  127.       case 'I':    outtype = FMT_IFF; break;
  128.       case 'J':    outtype = FMT_JPEG; break;
  129.       case 'L':    outtype = FMT_LEAF; break;
  130.       case 'M':    outtype = FMT_MCP; break;
  131.       case 'P':    outtype = FMT_PBM; break;
  132.       case 'R':    outtype = FMT_RLE; break;
  133.       case 'S':    outtype = FMT_SUN; break;
  134.       case 'T':    outtype = FMT_TIFF; break;
  135.       case 'X':    outtype = FMT_X11; break;
  136.       case 'Z':    outtype = FMT_PCX; break;
  137.  
  138.  
  139.       default:    fprintf (stderr, "%s", USAGE);
  140.             exit (1);
  141.         }
  142.       }
  143.     }
  144.     
  145.     if (argc > 0)    size = atoi (argv[0]);
  146.  
  147.     /* Default parms for algorithms */
  148.     if (parm <= -1e9)
  149.     { if      (alg == 'b') parm = 50.0;
  150.       else if (alg == 'c') parm = 4.0;
  151.       else if (alg == 't') parm = 128.0;
  152.     }
  153.  
  154.     /* Determine output height & width (oh*ow <= size) */
  155.     h = input.hdr.rows;
  156.     w = input.hdr.cols;
  157.  
  158.     if (size < 0)
  159.     { oh = h; ow = w; }
  160.     else
  161.     { ow = sqrt ((double) size * w / (h * input.hdr.aspect));
  162.       ow &= ~7;            /* Make width multiple of 8 */
  163.       oh = ow * input.hdr.aspect * h / w;
  164.     }
  165.  
  166.     fprintf (stderr,
  167.          "Halftone \"%s\" size [%dx%d] => %d pixels\n",
  168.          input.hdr.title[0] ? input.hdr.title : "(untitled)",
  169.          ow, oh, ow*oh);
  170.  
  171.     /* Start with image in variable 'input' */
  172.     image = &input;
  173.  
  174.     /* If necessary, resize it */
  175.     if (w != ow || h != oh)
  176.     { if (extract_fbm (image, &resized, 0, 0, w, h, ow, oh, title, credits))
  177.       { free_fbm (image);
  178.     image = &resized;
  179.       }
  180.       else
  181.       { exit (1); }
  182.     }
  183.  
  184.     /* Sharpen the image if requested */    
  185.     if (beta > -1e9)
  186.     { if (sharpen_fbm (image, &sharpened, beta))
  187.       { free_fbm (image);
  188.     image = &sharpened;
  189.       }
  190.       else
  191.       { exit (1); }
  192.     }
  193.     
  194.     /* Now use the appropriate algorithm to halftone it */
  195.     switch (alg)
  196.     { case 'b':    bluenoise_fbm (image, &halftoned, parm); break;
  197.       case 'c': constravg_fbm (image, &halftoned, parm); break;
  198.       case 't': thesh_fbm (image, &halftoned, (int) parm); break;
  199.       default:    floyd_fbm (image, &halftoned);
  200.     }
  201.  
  202.     /* free_fbm (image); */
  203.     image = &halftoned;
  204.  
  205.     if (clean >= 0)
  206.     { if (!clean_fbm (image, &cleaned, clean, 1, nbr))
  207.       { exit (1); }
  208.  
  209.       free_fbm (image);
  210.       image = &cleaned;      
  211.     }
  212.  
  213.     if (write_bitmap (image, stdout, outtype)) exit (0);
  214.   }
  215.   
  216.   exit (1);
  217. }
  218.